<template>
  <div class="departments-container">
    <el-card>
      <el-tabs v-model="activeName">
        <el-tab-pane label="组织结构" name="first" />
      </el-tabs>
      <div class="box">
        <!-- 标题 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end" style="text-align: right">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <el-col>{{ data.manager }}</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item
                      @click.native="addDep"
                    >添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <!-- 树形组件列表 -->
        <el-tree
          :data="data"
          :props="defaultProps"
          @node-click="handleNodeClick"
        >
          <template slot-scope="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end" style="text-align: right">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="addDep(data.id)"
                        >添加子部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="editDep(data)"
                        >查看部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="delDep(data.id)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
          <!--   <template #default="{ node, data }"> ==={{ data.label }} </template>-->
        </el-tree>
      </div>
      <el-dialog
        :title="isEdit ? '编辑部门':'添加部门'"
        :visible.sync="dialogVisible"
        width="50%"
        @close="closeDia"
      >
        <deptDialog
          v-if="dialogVisible"
          ref="dept"
          :is-edit="isEdit"
          :row="row"
          :visible.sync="dialogVisible"
          :pid="pid"
          @success="success"
        />
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from '../../api/departments'
import arrayToTree from 'array-to-tree'
import deptDialog from './dialog.vue'
export default {
  name: '',
  components: {
    deptDialog
  },
  data() {
    return {
      data: [
        {
          label: '一级 1',
          children: [
            {
              label: '二级 1-1',
              children: [
                {
                  label: '三级 1-1-1'
                }
              ]
            }
          ]
        },
        {
          label: '一级 2',
          children: [
            {
              label: '二级 2-1',
              children: [
                {
                  label: '三级 2-1-1'
                }
              ]
            },
            {
              label: '二级 2-2',
              children: [
                {
                  label: '三级 2-2-1'
                }
              ]
            }
          ]
        },
        {
          label: '一级 3',
          children: [
            {
              label: '二级 3-1',
              children: [
                {
                  label: '三级 3-1-1'
                }
              ]
            },
            {
              label: '二级 3-2',
              children: [
                {
                  label: '三级 3-2-1'
                }
              ]
            }
          ]
        }
      ],
      dialogVisible: false,
      activeName: 'first',
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      pid: '',
      isEdit: false,
      row: {}
    }
  },
  computed: {},
  created() {
    this.getDep()
  },
  methods: {
    handleNodeClick(data) {
      // console.log(data)
    },
    async getDep() {
      const res = await getDepartments()
      // console.log(res, 999999999)
      this.data = arrayToTree(res.data.depts, {
        parentProperty: 'pid',
        customID: 'id'
      })
    },
    addDep(pid) {
      this.dialogVisible = true
      this.pid = pid
      this.isEdit = false
    },
    success() {
      this.dialogVisible = false
      this.getDep() // 刷新页面
    },
    async delDep(id) {
      const res = await this.$confirm(
        '此操作将永久删除该文件, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch((err) => err)
      // console.log(`res`, res)
      if (res === 'confirm') {
        // 说明我点击的是确认按钮
        const res = await delDepartment(id)
        // console.log(res, 333)
        if (res.code === 10000) {
          this.getDep() // 刷新页面
        }
      }
    },
    editDep(row) {
      this.dialogVisible = true
      this.isEdit = true
      this.row = row
    },
    closeDia() {
      this.$refs.dept.close()
    }
  }
}
</script>
<style lang="scss" scoped>
.department-container {
  padding: 30px 50px;
}
.box {
  padding: 30px 50px;
}
</style>
